//
//  UserAccountTool.swift
//  weibo
//
//  Created by wang_h on 2017/5/31.
//  Copyright © 2017年 wang_h. All rights reserved.
//

import UIKit

class UserAccountViewModel {
    //MARK:- 将类设计成单例
    static let shareInstance : UserAccountViewModel = UserAccountViewModel()
    //MARK:- 定义属性
    var account : UserAccount?
    //MARK:- 计算属性
    var accountPath : String {
        //1.从沙盒中读取归档的信息
        //1.1.获取沙盒路径
        var accountPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        accountPath = (accountPath as NSString).appendingPathComponent("account.plist")
        return accountPath
    }
    
    var isLogin : Bool {
        if account == nil {
            return false
        }
        
        guard let expires_date = account?.expires_date else { return false}
        
        return expires_date.compare(NSDate() as Date) == ComparisonResult.orderedDescending
    }
    
    //MARK:- 重写init()函数
    init() {
        //1.从沙盒中读取归档的信息
        account = NSKeyedUnarchiver.unarchiveObject(withFile: accountPath) as? UserAccount
    }
}
